Create ROS2 Docker image
Table of Content
Following the ROS install instructions
- Base on ubuntu 22.04
- Install language
- Install timezone
- Install common application
- Create new source.list for ROS and install humble-base
Additional installs and settings
- Install some DEV tools like compiler, debugger and python pip
- Add user ros
FROM ubuntu:22.04 AS base
ENV DEBIAN_FRONTEND=noninteractive
# Install language
RUN apt-get update && apt-get install -y \
locales \
&& locale-gen en_US.UTF-8 \
&& update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 \
&& rm -rf /var/lib/apt/lists/*
ENV LANG en_US.UTF-8
# Install timezone
RUN ln -fs /usr/share/zoneinfo/UTC /etc/localtime \
&& export DEBIAN_FRONTEND=noninteractive \
&& apt-get update \
&& apt-get install -y tzdata \
&& dpkg-reconfigure --frontend noninteractive tzdata \
&& rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get -y upgrade \
&& rm -rf /var/lib/apt/lists/*
# Install common programs
RUN apt-get update && apt-get install -y --no-install-recommends \
curl \
gnupg2 \
lsb-release \
sudo \
software-properties-common \
wget \
&& rm -rf /var/lib/apt/lists/*
# Install ROS2
RUN sudo add-apt-repository universe \
&& curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(. /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null \
&& apt-get update && apt-get install -y --no-install-recommends \
ros-humble-ros-base \
python3-argcomplete \
&& rm -rf /var/lib/apt/lists/*
ENV ROS_DISTRO=humble
ENV AMENT_PREFIX_PATH=/opt/ros/humble
ENV COLCON_PREFIX_PATH=/opt/ros/humble
ENV LD_LIBRARY_PATH=/opt/ros/humble/lib
ENV PATH=/opt/ros/humble/bin:$PATH
ENV PYTHONPATH=/opt/ros/humble/lib/python3.10/site-packages
ENV ROS_PYTHON_VERSION=3
ENV ROS_VERSION=2
ENV DEBIAN_FRONTEND=
###########################################
# Develop image
###########################################
FROM base AS dev
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y --no-install-recommends \
bash-completion \
build-essential \
cmake \
gdb \
git \
openssh-client \
python3-argcomplete \
python3-pip \
ros-dev-tools \
vim \
&& rm -rf /var/lib/apt/lists/* \
&& rosdep init || echo "rosdep already initialized"
ARG USERNAME=ros
ARG USER_UID=1000
ARG USER_GID=$USER_UID
# Create a non-root user
RUN groupadd --gid $USER_GID $USERNAME \
&& useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME \
# [Optional] Add sudo support for the non-root user
&& apt-get update \
&& apt-get install -y sudo git-core bash-completion \
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME\
&& chmod 0440 /etc/sudoers.d/$USERNAME \
# Cleanup
&& rm -rf /var/lib/apt/lists/* \
&& echo "if [ -f /opt/ros/${ROS_DISTRO}/setup.bash ]; then source /opt/ros/${ROS_DISTRO}/setup.bash; fi" >> /home/$USERNAME/.bashrc
ENV DEBIAN_FRONTEND=
ENV AMENT_CPPCHECK_ALLOW_SLOW_VERSIONS=1